package team.gl.nio.cln;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import liuge.tools.Common;
import liuheClient.LiuheClient;
import team.gl.nio.cmn.Bag;

public class ClientHandler extends ChannelInboundHandlerAdapter {  
	
	long lastreconnecttime = 0;
	
	long lastUpdateTime = 0;
	
	public static ChannelHandlerContext ctx_s = null;	

    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg)  
            throws Exception {
	    	Bag us = (Bag)msg;
	
	    	if(us.getReq().equals("currentData")){
	    		LiuheClient.mergeData =  us.getCurrentData();
	    		if(LiuheClient.inHistoryView==false) {
	        		LiuheClient.mainWnd.updateCheckBoxes(LiuheClient.mergeData);
	        		LiuheClient.mainWnd.updateDatas(LiuheClient.mergeData);
	        		LiuheClient.mainWnd.updateTextArea(LiuheClient.mergeData);
	    		}
	    	}else if(us.getReq().equals("historyData")) {
	    		LiuheClient.inHistoryView = true;
	    		LiuheClient.historyData =  us.getCurrentData();
	    		if(LiuheClient.historyData==null) {
	    			JOptionPane.showMessageDialog(
	                        null, // 父组件
	                        "无法获取该时段数据", // 提示信息
	                        "错误", // 对话框标题
	                        JOptionPane.ERROR_MESSAGE // 错误消息类型
	                    );
	    		}else {
		    		LiuheClient.mainWnd.updateCheckBoxes(LiuheClient.historyData);
		    		LiuheClient.mainWnd.updateDatas(LiuheClient.historyData);
		    		LiuheClient.mainWnd.updateTextArea(LiuheClient.historyData);
		    		JOptionPane.showMessageDialog(
	                        null, // 父组件
	                        "获取历史数据成功", // 提示信息
	                        "成功", // 对话框标题
	                        JOptionPane.DEFAULT_OPTION // 错误消息类型
	                    );
	    		}

	    	}
    	}

    
    
    
    
    
	@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is active ---");
        ctx_s = ctx;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is inactive ---");
        NettyClient.channel = null;
        System.out.println("2s 涔嬪悗灏濊瘯閲嶆柊杩炴帴鏈嶅姟鍣�...");

        NettyClient.group.schedule(new Runnable() {          
            public void run() {  
                while(NettyClient.connect() == false) {
                	
                	try{  					
    					Thread.sleep(5000);
    					
    				}catch(Exception e){
    					e.printStackTrace();
    				} 
                }
                
                
                System.out.println("閲嶆柊杩炴帴鎴愬姛");
            }  
          
          }, 200L, TimeUnit.MILLISECONDS);  
          
        super.channelInactive(ctx);

    }
    
    
    
    
    
    
    

}